package zd.cornermemory.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import zd.cornermemory.R;
import zd.cornermemory.bean.CjMap;
import zd.cornermemory.utils.Statistics;
import zd.cornermemory.view.SectionPinAdapter;

public class PinAdapter extends SectionPinAdapter {
	private static final int VIEW_TYPE_ITEM_TIME    = 0;
	private static final int VIEW_TYPE_ITEM_CONTENT = 1;

	private List<CjMap> mData    = null;
	private Context     mContext = null;

	private OnClickItem onClickItem;

	public PinAdapter(Context context, List<CjMap> data) {
		mContext = context;
		mData = data;
	}

	public void setData(List<CjMap> data) {
		mData = data;
		notifyDataSetChanged();
	}

	public void setOnClickItem(OnClickItem onClickItem) {
		this.onClickItem = onClickItem;
	}

	@Override
	public int getCount() {
		return mData == null ? 0 : mData.size();
	}

	@Override
	public String getItem(int position) {
		return mData.get(position).getTitleName();
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public int getItemViewType(int position) {
		String title = mData.get(position).getTitleName();
		if (!TextUtils.isEmpty(title)) {
			return VIEW_TYPE_ITEM_TIME;
		}else{
			return VIEW_TYPE_ITEM_CONTENT;
		}
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View v = null;
		if (getItemViewType(position) == VIEW_TYPE_ITEM_TIME) {
			ViewSectionHolder viewHolder;
			if (convertView == null) {
				v = LayoutInflater.from(mContext).inflate(R.layout.item_adapter_title, parent, false);
				viewHolder = new ViewSectionHolder(v);
				v.setTag(viewHolder);
			} else {
				v = convertView;
				viewHolder = (ViewSectionHolder) v.getTag();
			}
			viewHolder.group_text.setText(getItem(position));
		} else if (getItemViewType(position) == VIEW_TYPE_ITEM_CONTENT) {
			ViewContentHolder viewHolder;
			if (convertView == null) {
				v = LayoutInflater.from(mContext).inflate(R.layout.record_item, parent, false);
				viewHolder = new ViewContentHolder(v);
				v.setTag(viewHolder);
			} else {
				v = convertView;
				viewHolder = (ViewContentHolder) v.getTag();
			}
			String time = Statistics.timeToString(mData.get(position).getAvg());
			CjMap cj = mData.get(position);
			long dateLong = cj.getCjList().get(cj.getCjList().size()-1).getDate();
			String date = Statistics.handleLongDate(dateLong);
			viewHolder.date_show.setText(date);
			viewHolder.time_show.setText(time);
			viewHolder.item_layout.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					if (onClickItem != null) {
						onClickItem.onClick(position);
					}
				}
			});
		}
		return v;
	}

	public interface OnClickItem{
		void onClick(int position);
	}

	@Override
	public boolean isSection(int position) {
		return getItemViewType(position) == VIEW_TYPE_ITEM_TIME;
	}

	private class ViewSectionHolder {

		private TextView group_text;

		ViewSectionHolder(View v) {
			group_text = (TextView) v.findViewById(R.id.group_text);
		}
	}

	private class ViewContentHolder {

		private TextView time_show,date_show;
		private ImageView todoimage;
		public RelativeLayout item_layout;

		ViewContentHolder(View v) {
			time_show = (TextView) v.findViewById(R.id.time_show);
			date_show = (TextView) v.findViewById(R.id.date_show);
			todoimage = (ImageView) v.findViewById(R.id.todoimage);
			item_layout = (RelativeLayout) v.findViewById(R.id.item_layout);
		}
	}
}
